/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cashmate.struts;

import com.cashmate.hibernate.dao.CreditmatrixDAO;
import com.cashmate.hibernate.dao.EmployeeDAO;
import com.cashmate.hibernate.dao.LoanapplicationDAO;
import com.cashmate.hibernate.dao.LoanverificationDAO;
import com.cashmate.hibernate.dao.MatrixcategoriesCriteriaDAO;
import com.cashmate.hibernate.pojo.Creditmatrix;
import com.cashmate.hibernate.pojo.CreditmatrixCriteria;
import com.cashmate.hibernate.pojo.Documents;
import com.cashmate.hibernate.pojo.Employees;
import com.cashmate.hibernate.pojo.Loanverification;
import com.cashmate.hibernate.pojo.MatrixcategoriesCriteria;
import com.opensymphony.xwork2.ActionSupport;
import java.util.ArrayList;
import java.util.List;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

/**
 *
 * @author Lee
 */
public class AddMatrix extends ActionSupport {

    private String[] rating;
    private String[] matrixcategid;
    private String[] criteriaid;
    private String successurl;
    //document
    private String docdate;
    private String docno;
    private String doctype;
    //loanapp
    private String loanverid;
    private String actionstatus;
    //employee
    private String employeeid;
    private String total;
    private LoanverificationDAO lvd = (LoanverificationDAO) ServletActionContext.getServletContext().getAttribute("loanverificationDAO");
    private LoanapplicationDAO ld = (LoanapplicationDAO) ServletActionContext.getServletContext().getAttribute("loanapplicationDAO");
    private EmployeeDAO emd = (EmployeeDAO) ServletActionContext.getServletContext().getAttribute("employeeDAO");
    private MatrixcategoriesCriteriaDAO mccd = (MatrixcategoriesCriteriaDAO) ServletActionContext.getServletContext().getAttribute("matrixcategoriesCriteriaDAO");
    private CreditmatrixDAO cmd = (CreditmatrixDAO) ServletActionContext.getServletContext().getAttribute("creditmatrixDAO");

    public void setDocno(String docno) {
        this.docno = docno;
    }

    public void setDoctype(String doctype) {
        this.doctype = doctype;
    }

    public void setEmployeeid(String employeeid) {
        this.employeeid = employeeid;
    }

    public void setLoanverid(String loanverid) {
        this.loanverid = loanverid;
    }

    public void setDocdate(String docdate) {
        this.docdate = docdate;
    }

    public void setTotal(String total) {
        this.total = total;
    }

    public String getSuccessurl() {
        return successurl;
    }

    public void setCriteriaid(String[] criteriaid) {
        this.criteriaid = criteriaid;
    }

    public void setMatrixcategid(String[] matrixcategid) {
        this.matrixcategid = matrixcategid;
    }

    public void setRating(String[] rating) {
        this.rating = rating;
    }
    
    public void setActionstatus(String actionstatus) {
        this.actionstatus = actionstatus;
    }

    public String getActionstatus() {
        return actionstatus;
    }

    @Action(value = "/add-matrix", results = {
        @Result(name = "input", location = "${successurl}"),
        @Result(name = "success", location = "${successurl}"),
        @Result(name = "error", location = "${successurl}")
    })
    @Override
    public String execute() {
        successurl = "../../loanapplication.jsp?cat=matrix";
        double doubletotal = 0;
        Loanverification loanver = null;
        Documents doc = null;
        Creditmatrix c = null;
        Employees emp = null;
        List<CreditmatrixCriteria> listcmc = new ArrayList<CreditmatrixCriteria>();

        try {
            doubletotal = Double.parseDouble(total);
            loanver = lvd.getById(Integer.parseInt(loanverid));
            emp = emd.getByIdNumber(employeeid);
            doc = new Documents(docno, doctype, docdate, true);
            c = new Creditmatrix(docdate, doubletotal);
            c.setLoanverifications(loanver);
            c.setDocuments(doc);
            c.setEmployees(emp);
        } catch (Exception e) {
//            System.out.println("Error in associating matrixcategriescriteria to creditmatrix.");
            e.printStackTrace();
            return ERROR;
        }


        if (rating.length == matrixcategid.length && rating.length == criteriaid.length) {
            for (int i = 0; i < rating.length; i++) {
                try {
                    int cat = Integer.parseInt(matrixcategid[i]);
                    int crit = Integer.parseInt(criteriaid[i]);

                    MatrixcategoriesCriteria m = mccd.getByCategoryAndCriteria(cat, crit);
                    CreditmatrixCriteria cmc = new CreditmatrixCriteria(m, c, Integer.parseInt(rating[i]));
                    listcmc.add(cmc);
                } catch (Exception e) {
//                    System.out.println("Error in associating matrixcategriescriteria to creditmatrix.");
                    e.printStackTrace();
                    return ERROR;
                }
            }
        }

        boolean condition;
        try {
            condition = cmd.addCreditMatrix(doc, c, listcmc);
        } catch (Exception e) {
            condition = false;
            e.printStackTrace();
//            System.out.println("Error in adding credit matrix.");
            return ERROR;
        }

        if (condition == true) {
            try {
                loanver.setWithcm("y");
                lvd.update(loanver);
//                System.out.println("NASAVE NA LAHAT.");
                actionstatus = "Appraisal saved!";
            } catch (Exception e) {
//                System.out.println("Error updating withcm status.");
                e.printStackTrace();
                return ERROR;
            }


        } else {
//            System.out.println("HINDI NASAVE.");
            return ERROR;
        }
        return SUCCESS;
    }
}
